//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        showKnows: true,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
      cardNumber: '',
      cardPassword: '',
    },
    //事件处理函数
    bindViewTap: function() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    cardNumberInput: function(e) {
        this.setData({
            cardNumber: e.detail.value
        })
    },
    cardPasswordInput: function(e) {
        this.setData({
            cardPassword: e.detail.value
        })
    },

    orderBuy: function() {
        if (!wx.getStorageSync('openId')){
            wx.showModal({
                title: '提示',
                content: '您还未登录，前往登录',
                success(res) {
                    if (res.confirm) {
                        wx.switchTab({
                            url: '/pages/my/my'
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
            return false
        }    

        if (!this.data.cardNumber) {
            wx.showToast({
                title: '卡卷号不能为空',
                icon: 'none'
            })
            return false
        }
        if (!this.data.cardPassword) {
            wx.showToast({
                title: '卡卷密码不能为空',
                icon: 'none'
            })
            return false
        }

        var data = {
            number: this.data.cardNumber,
            password: this.data.cardPassword,
        }
        app.request('api/VerCode/VerCodeInfo', data, 'Get',
            (result) => {
                if (result.data.Code == '0000') {
                  var that = this
                  wx.showModal({
                    title: '提示',
                    content: '预约成功，跳转下单！',
                    showCancel: false,
                    success(res) {
                      if (res.confirm) {
                        wx.navigateTo({
                          url: '/pages/order/order?cardNum=' + that.data.cardNumber,
                        })
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                    
                } else if (result.data.Code == '0417'){
                    wx.showToast({
                      title: result.data.Message,
                      icon:'none'
                    })
                }
                
                 else {
                    wx.showModal({
                        title: '提示',
                        content: '卡劵或密码错误！',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                }
            }
        );
        



    },
    onLoad: function() {
        // var data = { //接口测试
        //     openId: '',
        //     nickname: '',
        //     head_url: '',
        // }
        // app.request('api/VerCode/AddUser', data, 'GET',
        //     (result) => {
        //         console.log(result);
        //     }
        // );

        


        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
    },
  onShow: function () {
    this.setData({
      cardNumber: '',
      cardPassword: '',
    })

    if (!wx.getStorageSync('openId')) {
      wx.showModal({
        title: '提示',
        content: '您还未登录，前往登录',
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/my'
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }    
  },
    getUserInfo: function(e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    showKnowBtn: function() {
        this.setData({
            showKnows: false
        })
    },
    knows_confirm: function() {
        this.setData({
            showKnows: true
        })
    }
})